

$("#milestonePage").live('pageinit',function(event) {
	$.kulstats.milestones = {};
	$.kulstats.milestones.barsDiv = $("#milestones_bars_div");
	$.kulstats.milestones.list = new Array();
	
	$('#hours').slider("refresh");
	google.setOnLoadCallback(drawMilestones());
	
	$("#butt_mst_start").click(function(event) {
		$("#milestone_form").submit();
	});
	$("#milestone_form").submit(function(event) {
		event.preventDefault();
		$.post($("#milestone_form").attr("action"), $("#milestone_form").serialize(), function(resp) {
			resp = $.parseJSON(resp);
			$.kulstats.db3 = resp;
			if(resp.status == "success") {
				//success!
				drawMilestones();
			}
			else {
				alert("You cannot have 2 milestones of the same course.");
			}
		});
	});
	
});

function drawMilestones() {
	$.post("DoMilestone",{milestone_action: "milestones_get"},function(resp) {
		resp = $.parseJSON(resp);
		$.kulstats.db2 = resp;
		if(resp.status == "success") {
			var mls = resp.milestones;
			$.kulstats.db1 = mls;
			var barDiv = $.kulstats.milestones.barsDiv;
			barDiv.html("");
			//var data = new google.visualization.DataTable();
		    //data.addColumn('string', 'course');
		    //data.addColumn('number', 'progress');
			for(var i = 0; mls[i] ; i++) {
				//data.addRow([mls[i].course,mls[i].progress]);
				barDiv.append(
						$("<div/>")
						.attr("id","milestones_ms_"+i)
						.append(
								$("<p/>")
								.text(mls[i].course+" ("+mls[i].hours+" hour"+(mls[i].hours > 1 ? "s" : "")+")")
						)
						.append(
								$("<div/>")
								.attr("id","milestones_ms_"+i+"_bar")
						)					
				);
				$.kulstats.milestones.list[i] = {bar:$("#milestones_ms_"+i+"_bar"),ini:mls[i].progress,cur:mls[i].progress,tot:100};
				//$.kulstats.milestones.list[i].cur = 20;
				createProgress($.kulstats.milestones.list[i]);
			}
			/*var options = {cht: "bhs", chf: "bg,s,00000000",
				chd: "t:20,40,60,80,100",
				chdl: "", 
				chdls: "", 
				chdlp: "b|", 
				chp: 0, // zero-line
				title:'Unfinished Milestones',
				width:0.9*$(window).width(),
                hAxis: { title: "Progress %", minValue: 0, maxValue: 100}};
			// Instantiate and draw our chart, passing in some options.
			var chart = new google.visualization.ImageChart(document.getElementById('chart_div'));
			chart.draw(data, options);*/
		}
	});
}